﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace DaZhong
{
    public class Log
    {
        /// <summary>
        /// 记录成功日志,记录成功推送消息
        /// </summary>
        /// <param name="msg"></param>
        public static void WriteOK(string msg)
        {
            string filePath = GetDirectory("Log/");
            FileHelper.AppendText(filePath + DateTime.Now.ToString("yyyy_MM_dd") + ".log", DateTime.Now.ToString() + " " + msg + "\r\n");
        }
        public static void WriteNO(string msg)
        {
            FileHelper.AppendText(GetDirectory("Log/no/") + "no_" + DateTime.Now.ToString("yyyy_MM_dd") + ".log", DateTime.Now.ToString() + " " + msg + "\r\n");
        }
        public static void WriteErr(string msg)
        {
            FileHelper.AppendText(GetDirectory("Log/no/") + "err_" + DateTime.Now.ToString("yyyy_MM_dd") + ".log", DateTime.Now.ToString() + " " + msg + "\r\n");
        }
        public static void WriteErr(string fileName, string msg)
        {
            FileHelper.AppendText(GetDirectory("Log/err/") + fileName + DateTime.Now.ToString("yyyy_MM_dd") + ".log", DateTime.Now.ToString() + " " + msg + "\r\n");
        }
        public static void WriteServiceLog(string msg)
        {
            FileHelper.AppendText(GetDirectory("Log/service/") + DateTime.Now.ToString("yyyy_MM_dd") + ".log", DateTime.Now.ToString() + " " + msg + "\r\n");
        }

        public static void WriteServiceLogErr(string msg)
        {
            FileHelper.AppendText(GetDirectory("Log/service/") + "err_" + DateTime.Now.ToString("yyyy_MM_dd") + ".log", DateTime.Now.ToString() + " " + msg + "\r\n");
        }

        public static void WriteDBErr(string msg)
        {
            FileHelper.AppendText(GetDirectory("Log/db/") + "err_" + DateTime.Now.ToString("yyyy_MM_dd") + ".log", DateTime.Now.ToString() + " " + msg + "\r\n");
        }

        /// <summary>
        /// 检查当天是否推送过
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public static bool IsPush(int userId)
        {
            bool isTrue = false;
            string filePath = GetDirectory("Log/") + DateTime.Now.ToString("yyyy_MM_dd") + ".log";
            if (!File.Exists(filePath))
                return isTrue;
            StreamReader sr = null;
            try
            {
                sr = new StreamReader(filePath);
                while (sr.Peek() > -1)
                {
                    if (sr.ReadLine().IndexOf("ID:" + userId) > -1)    //如果今天推送了，退出
                    {
                        isTrue = true;
                        break;
                    }
                }
            }
            catch { }
            finally { if (sr != null) sr.Close(); }
            return isTrue;
        }

        static string GetDirectory(string path)
        {
            string filePath = AppDomain.CurrentDomain.BaseDirectory + path;
            if (!FileHelper.IsExistDirectory(filePath))
                FileHelper.CreateDirectory(filePath);
            return filePath;
        }
    }
}